package org.wxd.bi.test.code;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wxd.agent.AgentService;
import org.wxd.exception.Throw;
import org.wxd.lang.RunResult;
import org.wxd.net.web.hs.util.PostRunCode;

/**
 * @author: Troy.Chen(無心道, 15388152619)
 * @version: 2021-10-29 16:36
 **/
public class Agent extends PostRunCode {

    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(Agent.class);

    public static void main(String[] args) throws Throwable {
        AgentService.agentClass("4071", "lib/org.wxd.agent-2.3.8.jar", "target/classes");
    }

    @Test
    public void post0() throws Throwable {
        postParams.put("agentPath", "classes");
        postCode();
    }

    @Test
    public void post1() throws Throwable {
        postParams.put("agentPath", "target/classes");
        postCode();
    }

    @Override
    public RunResult run(JSONObject putData) throws Exception {
        try {
//            String pid = JvmUtil.processIDString();
//            log.error("进程id：" + pid);
//            List<AttachProvider> providers = AttachProvider.providers();
//            for (AttachProvider provider : providers) {
//                System.out.println(provider.name());
//                System.out.println(provider.type());
//                List<VirtualMachineDescriptor> virtualMachineDescriptors = provider.listVirtualMachines();
//                for (VirtualMachineDescriptor virtualMachineDescriptor : virtualMachineDescriptors) {
//                    String id = virtualMachineDescriptor.id();
//                    System.out.println(id);
//                    if (pid.equals(id)) {
//                        VirtualMachine virtualMachine = provider.attachVirtualMachine(virtualMachineDescriptor);
//                        System.out.println(Object2Json.toJson(virtualMachine));
//                    }
//                    System.out.println("----");
//                }
//                System.out.println("=======================================");
//            }
            String agentPath = putData.getString("agentPath");
            AgentService.agentClass("lib/org.wxd.agent-2.3.8.jar", agentPath);
            return RunResult.ok();
        } catch (Throwable throwable) {
            log.error("", throwable);
            return RunResult.of().setMsg(Throw.ofString(throwable));
        }
    }
}
